﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using System.Globalization;
using Helper;

public partial class Reports_ReducingBalance : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {

        /*set default values for the filter controls*/
        if (Page.IsPostBack == false)
        {
            /*Append page title*/
            Common.AppendPageTitle(Master, Globals.MasterPageHeaderID, " - Reducing Balance Report");

            /*hide the report container since the page was not posted back*/
            reportContainer.Visible = false;

            //reportDateContainer.Attributes.Add("data-date", DateTime.Now.ToString("dd-MM-yyyy"));
            reportDateContainer.Attributes.Add("data-date-format", "dd-mm-yyyy");
            txtReportDate.Value = DateTime.Now.ToString("dd-MM-yyyy");
        }
        else
        {
            
        }
    }

    protected void btnRunReport_Click(object sender, EventArgs e)
    {
        try
        {
            string reportDate = string.Empty;
            try
            {
                /*get year and month for which the report is generated - to be printed on the report*/
                reportDate = GetReportDate().ToString("MMMM yyyy");
            }
            catch (FormatException ex)
            {
                lblStatus.Text = "The date entered is invalid.";
                throw new Exception("The date entered is invalid", ex);
            }

            /*report parameter list*/
            List<ReportParameter> param = new List<ReportParameter>();

            /*create the report date parameter and set it on the report viewer -rvLoan*/
            param.Add(new ReportParameter("ReportDate", reportDate));
            rvLoan.LocalReport.SetParameters(param);

            reportContainer.Visible = true;

            /*display the report on the page*/
            rvLoan.LocalReport.Refresh();
        }
        catch (Exception ex)
        {
            throw new Exception("An error occured while generating the report", ex);
        }

    }

    protected void odsLoans_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        e.InputParameters["report_date"] = GetReportDate();
    }

    protected DateTime GetReportDate()
    {
        return DateTime.ParseExact(txtReportDate.Value.ToString(), "dd-MM-yyyy", CultureInfo.InvariantCulture);
    }
    protected void rvLoan_ReportError(object sender, ReportErrorEventArgs e)
    {
        Error appException = new Error(e.Exception.GetType().Name, e.Exception.Message, DateTime.Now, stackTrace: e.Exception.StackTrace, additionalDetails: e.Exception.Source);
        int logId = 0;
        try
        {
            ErrorLogBLL exceptionUtility = new ErrorLogBLL();
            logId = exceptionUtility.LogException(appException);
        }
        catch
        {

        }
        finally
        {
            if (e.Exception.InnerException.InnerException.Message.Contains("A network-related or instance-specific error"))
                errorMessage.InnerHtml = "An error occurred while processing the report.<br />" +
                    "Database not found.<br />" +
                    "Please notify the site administrator for assistance.";
            else if (e.Exception.InnerException.InnerException.Message.Contains("Cannot open database"))
                errorMessage.InnerHtml = "An error occurred while processing the report.<br />" +
                    "Unable to open database - Login Failed.<br />" +
                    "Please notify the site administrator for assistance.";
            else if (e.Exception.InnerException.InnerException.Message.Contains("Login failed"))
                errorMessage.InnerHtml = "An error occurred while processing the report.<br />" +
                    "Database login failed.<br />" +
                    "Please notify the site administrator for assistance.";
            else if (logId > 0)
                errorMessage.InnerHtml = "An error occurred while processing the report.<br />" +
                    "The error has been logged to the system with logId:" + logId + ".<br />" +
                    "Please notify the site administrator for assistance.";
            else
                errorMessage.InnerHtml = "An error occurred while processing the report.<br />" +
                    "Please notify the site administrator for assistance.";
            errorMessage.Visible = true;
            e.Handled = true;
        }
    }
}